package cn.ultrarisk.scadmin.manager.biz.common.valid;

import cn.ultrarisk.scadmin.manager.biz.common.dao.SchedulerJobDAO;
import cn.ultrarisk.scadmin.manager.exception.BizCheckedException;
import cn.ultrarisk.scadmin.manager.support.SpringContextHolder;
import cn.ultrarisk.scadmin.manager.util.StringUtil;
import org.quartz.CronExpression;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;

/**
 * SchedulerJobValidator
 *
 * @author sevendlong(lin23871@163.com)
 * @date 2014/5/29
 * @since V1.0
 */
@Component
public class SchedulerJobValidator {
    @Autowired
    private SchedulerJobDAO schedulerJobDAO;

    public void validName(String name) {
        if (StringUtil.isEmpty(name)) {
            throw new BizCheckedException("the name can't be empty");
        }
    }

    public void validBeanMethod(String bean, String method) {
        try {
            Object beanObj = SpringContextHolder.getBean(Class.forName(bean));
            Method methodObj = beanObj.getClass().getMethod(method);
            if (method == null) {
                throw new BizCheckedException("the bean's method was wrong");
            }
        } catch (Exception e) {
            throw new BizCheckedException("the bean's method was wrong");
        }
    }

    public void validExpression(String expression) {
        if (!CronExpression.isValidExpression(expression)) {
            throw new BizCheckedException("the expression was wrong");
        }
    }
}
